Skip to content

Add index parameter to filterMap callback signature#2

Merged
lohmander merged 3 commits intomainfrom
copilot/update-filtermap-signature
Nov 4, 2025
Merged

Add index parameter to filterMap callback signature#2
lohmander merged 3 commits intomainfrom
copilot/update-filtermap-signature

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

The filterMap method callback only received the array element value, inconsistent with standard JavaScript array methods like map and filter which provide both value and index.

Changes

  • Maybe.filterMap: Updated signature to fn: (value: U, index: number) => Maybe<V>
  • AsyncMaybe.filterMap: Updated signature to fn: (value: U, index: number) => Maybe<V>
  • Functional wrapper: Updated all filterMap overloads to include index parameter

Usage

// Filter by position
Maybe.fromNullable(['a', 'b', 'c', 'd'])
  .filterMap((x, i) => i % 2 === 0 ? Maybe.fromNullable(x) : Maybe.fromNullable(null))
  .value(); // ['a', 'c']

// Use index in transformation
AsyncMaybe.fromPromise(Promise.resolve([10, 20, 30]))
  .filterMap((x, i) => Maybe.fromNullable(x + i))
  .value(); // Promise<[10, 21, 32]>

The change is backward compatible—existing code without index parameter continues to work.

Original prompt

This section details on the original issue you should resolve

<issue_title>filterMap does not provide an index</issue_title>
<issue_description>Currently filterMap has the signature filterMap(value: T): ..., but for an array mapping function it should include the index of the map item, i.e., filterMap(value: T, index: number).</issue_description>

<agent_instructions>Applies to Maybe, AsyncMaybe, and the fp version</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits November 4, 2025 17:07
Co-authored-by: lohmander <2197050+lohmander@users.noreply.github.com>
- Updated Maybe.filterMap to accept (value, index) callback
- Updated AsyncMaybe.filterMap to accept (value, index) callback
- Updated functional filterMap wrapper to pass index parameter
- Added comprehensive tests for all three implementations
- All tests passing (109 tests)

Co-authored-by: lohmander <2197050+lohmander@users.noreply.github.com>
Copilot AI changed the title [WIP] Update filterMap to include index parameter Add index parameter to filterMap callback signature Nov 4, 2025
Copilot AI requested a review from lohmander November 4, 2025 17:14
@lohmander lohmander marked this pull request as ready for review November 4, 2025 17:17
@lohmander lohmander merged commit fea82cd into main Nov 4, 2025
1 check passed
@lohmander lohmander deleted the copilot/update-filtermap-signature branch November 4, 2025 17:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

filterMap does not provide an index

2 participants